/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package geneontofx.statistics;

import geneontofx.go.OntologyTerm;
import geneontofx.sets.ISet;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author loskov
 */
public class Test implements ITest{
    
    protected ISet study;
    protected ISet population;
    
    protected CorrectionType correctionType;
    
    protected HashMap<OntologyTerm, BigDecimal> pValues;
    protected HashMap<OntologyTerm, BigDecimal> adjustedPValues;
    
    
    public Test() {
        pValues = new HashMap<>();
        adjustedPValues = new HashMap<>();
        correctionType = CorrectionType.NONE;
    }

    public Test(ISet study, ISet population) {
        pValues = new HashMap<>();
        adjustedPValues = new HashMap<>();
        correctionType = CorrectionType.NONE;
        this.study = study;
        this.population = population;
    }

    @Override
    public CorrectionType getCorrectionType() {
        return correctionType;
    }

    @Override
    public void performTest() {
        ConcurrentHashMap<OntologyTerm, Integer> studyCounted = study.getCountedOntoTermsMap();
        ConcurrentHashMap<OntologyTerm, Integer> populationCounted = population.getCountedOntoTermsMap();
        
        for(Map.Entry<OntologyTerm, Integer> entry : studyCounted.entrySet()) {
            int N = population.getTotalCount();
            int m = study.getTotalCount();
            int n = populationCounted.get(entry.getKey());
            int x = studyCounted.get(entry.getKey());
            BigDecimal pv = new PValue().pValue(N, m, n, x);
            DecimalFormat frmt = new DecimalFormat("0.000E00");
            if(pv.compareTo(new BigDecimal("0.001")) < 1) System.out.println(entry.getKey().toString() + " " + frmt.format(pv.doubleValue()));
            pValues.put(entry.getKey(), pv);
            adjustedPValues = pValues;
        }
    }

    @Override
    public HashMap<OntologyTerm, BigDecimal> getCleanPValues() {
        return pValues;
    }

    @Override
    public HashMap<OntologyTerm, BigDecimal> getAdjustedPValues() {
        return adjustedPValues;
    }
    
    public void setStudy(ISet study) {
        this.study = study;
    }

    public void setPopulation(ISet population) {
        this.population = population;
    }
}
